<?php
namespace app\index\Controller;

use AlibabaCloud\Client\AlibabaCloud;
use app\common\controller\BaseController;
use app\index\model\Users;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use think\auth\Auth;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Loader;
use think\Request;
use think\Response;
use think\Session;

class Ajax extends BaseController
{
    //点击激活
    public function Ajaxa()
    {
        $post = Request::instance()->post();
        if (!empty($post)) {
            $a = $post['status'];
            $id = $post['id'];
            if ($a == '关闭') {
                Db::table('app_users')->where('id', $id)->update(['status' => '0']);
                echo 0;
            }
            if ($a == '激活') {
                Db::table('app_users')->where('id', $id)->update(['status' => '1']);
                echo 1;
            }
        }
    }

//失焦修改电话号
    public function tel()
    {
        $post = Request::instance()->post();
        if (!empty($post)) {
            $tel = $post['mobile'];
            $id = $post['id'];

            Db::table('app_users')->where('id', $id)->update(['mobile' => $tel]);
            $data = [
                'id' => 1
            ];

//        return Response::create($data,'json','404');
            return json($data);

        }

    }

//ajax上传
//    public function ajaxUpload()
//    {
//        $request = Request::instance();
//        $post = $request->post();
//
//
//        if ($request->isPost()) {
//
//
//            $validate = Loader::validate('AddUser');
//            $yan = $validate->check($post);
//
//            if (false === $yan) {
//                $this->error($validate->getError());
//            }
//            if ($request->isPost()) {
//                $data = [];
//                $info = $this->upload_file([], 'upload', 'logo');
//
//                if (false === $info) {
//                    $data = [
//                        'status' => 0,
//                        'error' => '上传文件失败!' . $this->error,
//                        'pic_url' => '',
//                        'thumb_url' => '',
//                    ];
//                    return json($data);
//                }
//                $pic_url = str_replace(DS, '/', 'upload' . DS . $info->getSaveName());
//                $fileArr = explode('/', $pic_url);
////        return json($fileArr);
//                $thumb_url = $fileArr[0] . '/' . $fileArr[1] . '/' . 'thumb_' . $info->getFileName();
//                try {
//                    $water = \think\Config::get('upload_water');
//                    \think\Image::open($pic_url)->thumb(150, 150)->save($thumb_url);
//                    \think\Image::open($pic_url)->water($water)->save($pic_url);
//                    $data = [
//                        'status' => '1',
//                        'error' => '上传成功!',
//                        'pic_url' => $pic_url,
//                        'thumb_url' => $thumb_url,
//                    ];
//                    return json($data);
//                } catch (\Exception $e) {
//                    $data = [
//                        'status' => 0,
//                        'error' => '上传文件处理失败!' . $e->getMessage(),
//                        'pic_url' => '',
//                        'thumb_url' => '',
//                    ];
//                    return json($data);
//                }
//            }
////            if (!empty($post['username'])) {
////                $user = new Users();
////                $result = $user->adduser($post);
////
////                if (false === $result) {
////                    $this->error('新增失败!' . $user->getError());
////
////                } else {
////
////                    $this->success('新增成功! ID:' . $result, 'index/Index/info');
////                }
//
////
////            }
////
////
////        }
//
//        }
//    }

    public function ajaxUpload()
    {
        $request = Request::instance();
//        if($request->isPost()){
        $data = [];
        if ($request->isPost()) {
            $info = $this->upload_file([], 'upload', 'logo');
            if (false === $info) {
                $data = [
                    'status' => 0,
                    'error' => '上传文件失败！' . $this->error,
                    'pic_url' => '',
                    'thumb_url' => ''
                ];
                return json($data);
            }

            $pic_url = str_replace(DS, '/', 'upload' . DS . $info->getSaveName());

            $fileArr = explode('/', $pic_url);
            $thumb_url = $fileArr[0] . '/' . $fileArr[1] . '/' . 'thumb_' . $info->getFileName();

            try {
                $water = \think\Config::get('upload_water');
                \think\Image::open($pic_url)->thumb(150, 150)->save($thumb_url);
                \think\Image::open($pic_url)->water($water)->save($pic_url);

                $data = [
                    'status' => 1,
                    'error' => '上传成功!',
                    'pic_url' => $pic_url,
                    'thumb_url' => $thumb_url,
                ];
                return json($data);
            } catch (\Exception $e) {
                $data = [
                    'status' => 0,
                    'error' => '上传文件处理失败！!!!' . $e->getMessage(),
                    'pic_url' => '',
                    'thumb_url' => '',
                ];
                return json($data);
            }
        }
    }



//阿里云发短信
//    public function duanxin($phone){
//
//        $co=['code'=>'1234'];
//        json_encode($co);
//
//        $rt=$this->alisms($phone,$co);
//
//        echo "<pre>";
//        print_r($rt);
//        echo "</pre>";
//    }

    public function msg(){
        $request=Request::instance();
        if($request->isPost()){
            $post=$request->post();
            $sjs=$post['sjs'];
            $sjh=$post['sjh'];

            $root= \think\Config::get('aliyunsms');
            Cookie::set('yzm',$sjs);
            Cookie::set('yzm_time',time());
            $code=['code'=>$sjs];
            $code=json_encode($code);
            AlibabaCloud::accessKeyClient($root['accessKeyId'], $root['accessKeySecret'])
                ->regionId('cn-hangzhou')
                ->asDefaultClient();
            try {
                $result = AlibabaCloud::rpc()
                    ->product('Dysmsapi')
                    ->version('2017-05-25')
                    ->action('SendSms')
                    ->method('POST')
                    ->host('dysmsapi.aliyuncs.com')
                    ->options([
                        'query' => [
                            'RegionId' => "cn-hangzhou",
                            'PhoneNumbers' => $sjh,
                            'SignName' => $root['SignName'],
                            'TemplateCode' => $root['TemplateCode'],
                            'TemplateParam' => $code,
                        ],
                    ])
                    ->request();
                $arr=$result->toArray();

                return json($arr);

            } catch (ClientException $e) {
                echo $e->getErrorMessage() . PHP_EOL;
            } catch (ServerException $e) {
                echo $e->getErrorMessage() . PHP_EOL;
            }
        }
    }





}